﻿var controller = this.lookupController();
var vm = this.lookupViewModel();
//进行冻结操作
var ukCrossCharge = ref.UKCrossCharge.getValue();
//是否扣减
var isDeduction = ['1', '2', '3', '6'].includes(ref.ExpenseType.getValue());
var _company = ref.companyC.getValue();

if (isDeduction && _company == 'JLRC') {
    if (ukCrossCharge == 'UKJLRC' || ukCrossCharge == 'No') {

        var store3 = ref.datagrid3.getStore();
        var year = ref.fiscalYear.getValue().split('_')[0];

        Ext.each(store3.data.items, function (record) {
            var currentData = record.data;
            var company = currentData.Company;


            if (currentData.budgetControl == 'Y' && company == 'JLRC') {
                var pa = currentData.ProjectActivity;

                var paList = vm.get('NebulogyForm.ProjectActivityList');
                var paId = paList.filter((r) => { return r.ProjectActivity == pa })[0].ID;

                var budgetitemId = ref.BudgetItems.getValue();
                var cost = currentData.Cost;

                //冻结
                Ext.PAIRest.request({
                    ns: PAI.application.serviceUrl,
                    url: 'jlr-rest/api/budgetFmebugetItem/updateFreezingAmount',
                    method: "POST",
                    async: false,
                    data: {
                        budgetitemId: budgetitemId,
                        freezeOrUnfreeze: "0",
                        projectActivityId: paId,
                        thawAmount: cost,
                        company: company,
                        yearVal: year,
                        folio: '',
                        actInstId: '',
                        actionName: 'submit',
                        fromBalance: currentData.Balance
                    },
                    success: function (data) { },
                    failure: function (jsondata) { }
                });
            }
        });
    }
}

var store = ref.ExpenseType.getValue() == '4' ? ref.datagrid7.getStore() : ref.datagrid2.getStore();
var arr = [];

var paList = vm.get('NebulogyForm.ProjectActivityList');

Ext.each(store.data.items, function (record) {

    var paItem = paList.filter(function (data) {
        return data.ProjectActivity == record.get('ProjectActivity');
    });

    if (paItem.length) {
        arr.push(paItem[0].ID);
    }
});

Ext.PAIRest.request({
    ns: PAI.application.serviceUrl,
    url: 'jlr-rest/api/budgetFmebugetItem/updateLockStatus',
    method: "PUT",
    async: false,
    data: {
        "paIds": arr.join(','),
        "biId": ref.BudgetItems.getValue()
    },
    success: function (data) { },
    failure: function (jsondata) { }
});